<?php
global $_W,$_GPC;
load()->func('tpl');
$op = empty($_GPC['op'])?'list':$_GPC['op'];
if($op == 'list'){
    $params = array('weid'=>$_W['uniacid']);
    $modulelist = pdo_fetchall("SELECT * FROM ".tablename('goouc_class_module')." WHERE weid = :weid  ORDER BY `order` DESC",$params);
}elseif($op == 'post'){
    $id = intval($_GPC['id']);
    $info = pdo_fetch("SELECT * FROM ".tablename('goouc_class_module')." WHERE id = :id AND weid = :weid",array(':weid'=>$_W['uniacid'],':id'=>$id));
    if(checksubmit('submit')){
        if(empty($_GPC['name'])){
            message('模块名不能为空','','error');
        }
        $data['weid'] = $_W['uniacid'];
        $data['name'] = trim($_GPC['name']);
        $data['order'] = intval($_GPC['order']);
        $data['num'] = intval($_GPC['num']);
        $data['status'] = intval($_GPC['status']);
        $data['image'] = trim($_GPC['image']);
        $data['addtime'] = time();
        if ($this->userid == $_W['uid']) {
            message($this->msg);
        }
        if(empty($id)){
            $res = pdo_insert('goouc_class_module',$data);
            if($res){
                $arr = array(
                    'weid'=>$_W['uniacid'],
                    'admin_uid'=>$_W['uid'],
                    'admin_username'=>$_W['username'],
                    'log_type'=>1,
                    'function'=>'推荐模块->模块设置',
                    'content'=>'新增推荐模块'
                );
                $this->addlog($arr);
                message('新增模块成功',$this->createWebUrl('module'),'succss');
            }else{
                message('操作失败','','error');
            }
        }else{
            $res = pdo_update('goouc_class_module',$data,array('id'=>$id,'weid'=>$_W['uniacid']));
            if($res){
                $arr = array(
                    'weid'=>$_W['uniacid'],
                    'admin_uid'=>$_W['uid'],
                    'admin_username'=>$_W['username'],
                    'log_type'=>3,
                    'function'=>'推荐模块->模块设置',
                    'content'=>'修改模块信息'
                );
                $this->addlog($arr);
                message('修改模块成功',$this->createWebUrl('module'),'succss');
            }else{
                message('操作失败','','error');
            }
        }
    }
}elseif($op == 'delete'){
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    $mid = intval($_GPC['id']);
    $has = pdo_getall('goouc_class_lesson',array('moduleid'=>$mid,'weid'=>$_W['uniacid']));
    if($has){
        itoast('请先将模块下的课程移除！！！');
    }
    $res = pdo_delete('goouc_class_module',array('id'=>$mid,'weid'=>$_W['uniacid']));
    if($res){
        $arr = array(
            'weid'=>$_W['uniacid'],
            'admin_uid'=>$_W['uid'],
            'admin_username'=>$_W['username'],
            'log_type'=>2,
            'function'=>'推荐模块->模块设置',
            'content'=>'删除模块信息'
        );
        $this->addlog($arr);
        message('删除模块成功',$this->createWebUrl('module'),'succss');
    }else{
        message('操作失败','','error');
    }
    message('操作成功',$this->createWebUrl('module'),'success');
}elseif($op == 'lessonlist'){
    $mid = intval($_GPC['id']);
    $lesson_list = pdo_getall('goouc_class_lesson',array('moduleid'=>$mid,'weid'=>$_W['uniacid']));
    foreach($lesson_list as $k=>$v){
        $lesson_list[$k]['snum'] = count(pdo_getall('goouc_class_son',array('lessonid'=>$v['id']),array('id')));
    }
}elseif($op == 'removelesson'){
    $lid = $_GPC['id'];
    $mid = $_GPC['mid'];
    $data['moduleid'] = 0;
    if ($this->userid == $_W['uid']) {
        message($this->msg);
    }
    $res = pdo_update('goouc_class_lesson',$data,array('id'=>$lid,'weid'=>$_W['uniacid']));
    if($res){
        message('移除成功',$this->createWebUrl('module',array('op'=>'lessonlist','id'=>$mid)));
    }
}
include $this->template('web/module');
?>